<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page import = "com.classes.Casemast, java.util.ArrayList, java.util.TreeMap, java.text.SimpleDateFormat, java.util.Date, org.owasp.encoder.Encode" %>
<%@ page import = "org.json.simple.*" %>
<%@ page import = "com.mbbmap.util.Constants" %>
<%@ page import = "com.mbbmap.security.dao.*" %>
<%@ include file="/jsp/map/include/user-dao.jsp" %>	
<%@page import="com.mbbmap.util.EncryptionHelper"%>
<%

TreeMap<String, String> search = (TreeMap<String, String>) request.getAttribute("search");

%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<jsp:include page="/jsp/map/include/html-meta.jsp" />
<title>MAYBANK</title>
<script src="<%= request.getContextPath() %>/js/application.js"></script>
<script src="<%= request.getContextPath() %>/js/visa_vmts.js?v=20150305"></script>
</head>
<body onload="application.pending_completion_init()">

<!-- Header -->
<%@ include file="/jsp/common/Header.jsp" %>	
<!-- End Header -->


<div class="row-fluid page" style="width:1200px">
	<div class="span12">
	
		<jsp:include page="/jsp/map/include/visa-vmts-batch-tab.jsp">
			<jsp:param name="active" value='<%=EncryptionHelper.encrypt("pending") %>' />
		</jsp:include>
		
		<form name="search_form" method="post" action="<%= request.getContextPath() + "/VisaVMTSBatch.do?dispatch=init" %>">
			<input type="hidden" name="<%= Constants.TOKEN_KEY %>" value="<%= session.getAttribute(Action.TRANSACTION_TOKEN_KEY) %>" >
			<div class="row-fluid form-horizontal search">
				<div class="span6">
					
					<div class="control-group">
						<label class="control-label">Reference No.</label>
						<div class="controls">
							<%
							String html = "";
							if(search.containsKey("case_id") && search.get("case_id").length() > 0){
								html = search.get("case_id");
							}
							%>
							<input type="text" id="case_id" name="case_id" value="<%= html %>" />
						</div>
					</div>
					
					<div class="control-group">
						<label class="control-label">Registration Name</label>
						<div class="controls">
							<%
							html = "";
							if(search.containsKey("merchant_name") && search.get("merchant_name").length() > 0){
								html = search.get("merchant_name");
							}
							%>
							<input type="text" id="merchant_name" name="merchant_name" value="<%= html %>" />
						</div>
					</div>
					
					<div class="control-group">
						<label class="control-label">Application Type</label>
						<div class="controls">
							<select id="application_type" name="application_type">
							<% 
								html = "<option value=\"\"></option>"
									+ "<option value=\"New Application\">New Application</option>"
									+ "<option value=\"Extend Facility\">Extend Facility</option>"
									+ "<option value=\"e-Commerce/M2u\">e-Commerce/M2u</option>"
									+ "<option value=\"Additional Terminal/mPOS Reader\">Additional Terminal/mPOS Reader</option>";
								
								if(search.containsKey("application_type") && search.get("application_type").length() > 0){
									html = html.replace("value=\"" + search.get("application_type") + "\"", "value=\"" + search.get("application_type") + "\" selected ");
								}
								
								out.println(html);
							%>
							</select>
						</div>
					</div>
					
					<div class="control-group">
						<label class="control-label">Status</label>
						<div class="controls">
							<select id="status" name="status">
							<% 
								html = "<option value=\"\"></option>";
								
								for(String s : (ArrayList<String>) request.getAttribute("statusList")){
									if(s.indexOf("CLOSE") < 0) {
										html += "<option value=\"" + s + "\">" + s + "</option>";
									}
								}
								
								if(search.containsKey("status") && search.get("status").length() > 0){
									html = html.replace("value=\"" + search.get("status") + "\"", "value=\"" + search.get("status") + "\" selected ");
								}
								
								out.println(html);
							%>
							</select>
						</div>
					</div>
				</div>
				
				<div class="span6">
					
					<div class="control-group">
						<label class="control-label">Group</label>
						<div class="controls">
							<select id="group_code" name="group_code">
							<% 
								html = "<option value=\"\"></option>";
								for(SecGroupsDao ug : (ArrayList<SecGroupsDao>) request.getAttribute("groupList")){
									html += "<option value=\"" + ug.getGroupCode() + "\">" + ug.getGroupName() + "</option>";
								}
								
								if(search.containsKey("group_code") && search.get("group_code").length() > 0){
									html = html.replace("value=\"" + search.get("group_code") + "\"", "value=\"" + search.get("group_code") + "\" selected ");
								}
								
								out.println(html);
							%>
							</select>
						</div>
					</div>
					
					
					<div class="control-group">
						<label class="control-label">User</label>
						<div class="controls">
							<select id="group_user" name="group_user">
							<%
								html = "<option value=\"\"></option>";
								for(SecUserDao u : (ArrayList <SecUserDao>) request.getAttribute("userList")){
									html += "<option value=\"" + u.getLogonId() + "\">" + u.getUserName() + "</option>";
								}
								
								if(search.containsKey("group_user") && search.get("group_user").length() > 0){
									html = html.replace("value=\"" + search.get("group_user") + "\"", "value=\"" + search.get("group_user") + "\" selected ");
								}
								
								out.println(html);
							%>
							</select>
						</div>
					</div>
					
					<div class="control-group">
						<label class="control-label">Region</label>
						<div class="controls">
							<select id="reseller_location" name="reseller_location">
							<% 
								html = "<option value=\"\"></option>"
									+ "<option value=\"SABAH\">SABAH</option>"
									+ "<option value=\"SARAWAK\">SARAWAK</option>"
									+ "<option value=\"PERLIS\">PERLIS</option>"
									+ "<option value=\"KEDAH\">KEDAH</option>"
									+ "<option value=\"PULAU PINANG\">PULAU PINANG</option>"
									+ "<option value=\"PERAK\">PERAK</option>"
									+ "<option value=\"KELANTAN\">KELANTAN</option>"
									+ "<option value=\"TERENGGANU\">TERENGGANU</option>"
									+ "<option value=\"PAHANG\">PAHANG</option>"
									+ "<option value=\"SELANGOR\">SELANGOR</option>"
									+ "<option value=\"NEGERI SEMBILAN\">NEGERI SEMBILAN</option>"
									+ "<option value=\"MELAKA\">MELAKA</option>"
									+ "<option value=\"JOHOR\">JOHOR</option>"
									+ "<option value=\"WILAYAH PERSEKUTUAN\">WILAYAH PERSEKUTUAN</option>";
								
								if(search.containsKey("reseller_location") && search.get("reseller_location").length() > 0){
									html = html.replace("value=\"" + search.get("reseller_location") + "\"", "value=\"" + search.get("reseller_location") + "\" selected ");
								}
								
								out.println(html);
							%>
							</select>
						</div>
					</div>
					
				</div>
			</div>
		
		
			<div class="row-fluid">
				<div class="span12 btn-control search">
					<button id="search_submit" name="search_submit" type="submit" class="btn">Search</button>
					<button id="search_clear" name="search_clear" type="submit" class="btn">Clear</button>
				</div>
			</div>
		</form>
		
		<form name="search_form2" method="post" action="<%= request.getContextPath() + "/VisaVMTSBatch.do?dispatch=init" %>">
			<button class="btn" type="button" onclick="visa_vmts.batch_update()">Data Updated</button>
			<button id="export_submit" name="export_submit" class="btn" type="submit">Export Data</button>
		</form>
		
		<table class="tblDetails visavmtsbatch"><thead>
			<tr>
				<th>Batch Date</th>
				<th>Record Type</th>
				<th>Identification Type</th>
				<th>Merchant Country Group</th>
				<th>Client BID</th>
				<th>Merchant Legal Name</th>
				<th>Merchant Trading Name</th>
				<th>Business Registration No.</th>
				<th>Merchant Street Address</th>
				<th>Merchant City</th>
				<th>Merchant State/Province</th>
				<th>Merchant Postal Code</th>
				<th>Merchant Country</th>
				<th>Merchant Email</th>
				<th>Acceptance Channel</th>
				<th>Acquirer Country Code</th>
				<th>Merchant Category Code</th>
				<th>Internet URL</th>
				<th>IP Address 1</th>
				<th>IP Address 2</th>
				<th>IP Address 3</th>
				<th>Listing Reason Code</th>
				<th>Listing Reason Code Other</th>
				<th>Contract Open Date</th>
				<th>Contract Close Date</th>
				<th>Merchant Telephone Number 1</th>
				<th>Merchant Telephone Number 2</th>
				<th>Merchant Telephone Number 3</th>
				<th>Bank Account No. 1</th>
				<th>Bank Account No. 2</th>
				<th>Bank Account No. 3</th>
				<th>IPSP Name 1</th>
				<th>IPSP Name 2</th>
				<th>IPSP Name 3</th>
				<th>Reserved for future use 1</th>
				<th>Reserved for future use 2</th>
				<th>Reserved for future use 3</th>
				<th>Reserved for future use 4</th>
				<th>Reserved for future use 5</th>
				<th>Reserved for future use 6</th>
				<th>Reserved for future use 7</th>
				<th>Reserved for future use 8</th>
				<th>Reserved for future use 9</th>
				<th>Reserved for future use 10</th>
				<th>Principal/Owner 1 Name</th>
				<th>Principal/Owner 1 ID</th>
				<th>Principal/Owner 1 Contact No. 1</th>
				<th>Principal/Owner 1 Contact No. 2</th>
				<th>Principal/Owner 1 Contact No. 3</th>
				<th>Principal/Owner 1 Email</th>
				<th>Principal/Owner 2 Name</th>
				<th>Principal/Owner 2 ID</th>
				<th>Principal/Owner 2 Contact No. 1</th>
				<th>Principal/Owner 2 Contact No. 2</th>
				<th>Principal/Owner 2 Contact No. 3</th>
				<th>Principal/Owner 2 Email</th>
				<th>Principal/Owner 3 Name</th>
				<th>Principal/Owner 3 ID</th>
				<th>Principal/Owner 3 Contact No. 1</th>
				<th>Principal/Owner 3 Contact No. 2</th>
				<th>Principal/Owner 3 Contact No. 3</th>
				<th>Principal/Owner 3 Email</th>
			</tr>
		</thead><tbody>
		<%
			int n = 0;
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			
			for(TreeMap<String, String> c : (ArrayList<TreeMap<String, String>>) request.getAttribute("cArr")){
				String acquirer_country_code = "&nbsp;";
				String merchant_country_code = "";
				String contract_open_date = sdf.format(new Date());
				String contract_close_date = sdf.format(new Date());
				
				if(c.get("country_code") != null) {
					if(c.get("country_code").equals("PH")){
						acquirer_country_code = "608";
						merchant_country_code = "608";
					} else if(c.get("country_code").equals("SG")){
						acquirer_country_code = "702";
						merchant_country_code = "702";
					} else if(c.get("country_code").equals("MY")){
						acquirer_country_code = "458";
						merchant_country_code = "458";
					}
				}
				
				
				String line_1 = "";
				String line_2 = "";
				String line_3 = "";
				String line_4 = "";
				String postal_code = "";
				String city = "";
				String region_code = "";
				
				if(c.get("address") != null && c.get("address").length() > 0){
					JSONObject obj = (JSONObject) JSONValue.parse(c.get("address"));
					
					if(obj != null) {
						line_1 = (obj.get("line_1") == null) ? "" : obj.get("line_1").toString().trim();
						line_2 = (obj.get("line_2") == null) ? "" : obj.get("line_2").toString().trim();
						line_3 = (obj.get("line_3") == null) ? "" : obj.get("line_3").toString().trim();
						line_4 = (obj.get("line_4") == null) ? "" : obj.get("line_4").toString().trim();
						postal_code = (obj.get("postal_code") == null) ? "" : obj.get("postal_code").toString().trim();
						city = (obj.get("city") == null) ? "" : obj.get("city").toString().trim();
						region_code = (obj.get("region_code") == null) ? "" : obj.get("region_code").toString().trim();
						
						if(region_code.equals("002")) {
							region_code = "Sabah";
						} else if(region_code.equals("003")) {
							region_code = "Sarawak";
						} else if(region_code.equals("004")) {
							region_code = "Perlis";
						} else if(region_code.equals("005")) {
							region_code = "Kedah";
						} else if(region_code.equals("006")) {
							region_code = "Pulau Pinang";
						} else if(region_code.equals("007")) {
							region_code = "Perak";
						} else if(region_code.equals("008")) {
							region_code = "Kelantan";
						} else if(region_code.equals("009")) {
							region_code = "Terengganu";
						} else if(region_code.equals("010")) {
							region_code = "Pahang";
						} else if(region_code.equals("011")) {
							region_code = "Selangor";
						} else if(region_code.equals("012")) {
							region_code = "Negeri Sembilan";
						} else if(region_code.equals("013")) {
							region_code = "Melaka";
						} else if(region_code.equals("014")) {
							region_code = "Johor";
						} else if(region_code.equals("015")) {
							region_code = "Wilayah Persekutuan";
						}
						
					} 
				}
				
				
				String owner_name_1 = "";
				String owner_id_1 = "";
				String owner_contact_1 = "";
				String owner_email_1 = "";
				String owner_name_2 = "";
				String owner_id_2 = "";
				String owner_contact_2 = "";
				String owner_email_2 = "";
				String owner_name_3 = "";
				String owner_id_3 = "";
				String owner_contact_3 = "";
				String owner_email_3 = "";
				
				if(c.get("match_trace") != null && c.get("match_trace").length() > 0){
					JSONArray arr = (JSONArray) JSONValue.parse(c.get("match_trace"));
					
					for(int k = 0; k < arr.size(); k++) {
						String string = arr.get(k).toString();
				    	JSONObject obj = (JSONObject)JSONValue.parse(string);
				    	
						String f_name = (obj.get("f_name") == null) ? "" : obj.get("f_name").toString().trim();
				    	String l_name = (obj.get("l_name") == null) ? "" : obj.get("l_name").toString().trim();
				    	String ic = (obj.get("ic") == null) ? "" : obj.get("ic").toString().trim();
				    	
				    	if(k == 0){
					    	owner_name_1 = f_name + " " + l_name;
					    	owner_id_1 = ic;
				    	}
				    	else if(k == 1){
				    		owner_name_2 = f_name + " " + l_name;
					    	owner_id_2 = ic;
				    	}
				    	else if(k == 2){
				    		owner_name_3 = f_name + " " + l_name;
					    	owner_id_3 = ic;
				    	}
				    	else {
				    		break;
				    	}
					}
				}
				
				String case_url = request.getContextPath() + "/NewApp.do?dispatch=init&m_id=" + c.get("merchant_id") + "&c_id=" + c.get("id");
				
				int batch_id = Integer.parseInt(c.get("visa_vmts_batch"));
				String css_tr = "";
				if(batch_id > 0) {
					css_tr = "style=\"background-color:#f5f5f5\"";
				}
				
				String batch_date = "*";
				if(c.get("vmts_batch_date") != null) {
					batch_date = Casemast.formatCreatedDate("dd/MM/yyyy", Long.parseLong(c.get("vmts_batch_date")));
				}
				
				html = "<tr " + css_tr + " data-caseid=\"" + c.get("id") + "\" data-batchid=\"" + c.get("visa_vmts_batch") + "\">"
				+ "	<td>" + batch_date + "</td>"
				+ "	<td>T</td>"
				+ "	<td>S</td>"
				+ "	<td>1</td>"
				+ "	<td>&nbsp;</td>"
				+ "	<td><a href=\"" + case_url + "\">" + Encode.forHtml(c.get("registered_name")) + "</a></td>"
				+ "	<td>Internet Payment</td>"
				+ "	<td>" + (c.get("registered_no").equals("") ? "" : "000" + Encode.forHtml(c.get("registered_no"))) + "</td>"
				+ "	<td>" + line_1 + "</td>"
				+ "	<td>" + city + "</td>"
				+ "	<td>" + region_code + "</td>"
				+ "	<td>" + postal_code + "</td>"
				+ "	<td>" + merchant_country_code + "</td>"
				+ "	<td>" + Encode.forHtml(c.get("email")) + "</td>"
				+ "	<td>MOTO</td>"
				+ "	<td>" + acquirer_country_code + "</td>"
				+ "	<td>" + c.get("mcc_code") + "</td>"
				+ "	<td>&nbsp;</td>"
				+ "	<td>&nbsp;</td>"
				+ "	<td>&nbsp;</td>"
				+ "	<td>&nbsp;</td>"
				+ "	<td>&nbsp;</td>"
				+ "	<td>&nbsp;</td>"
				+ "	<td>" + contract_open_date + "</td>"
				+ "	<td>" + contract_close_date + "</td>"
				+ "	<td>" + c.get("tel_no") + "</td>"
				+ "	<td>&nbsp;</td>"
				+ "	<td>&nbsp;</td>"
				+ "	<td>&nbsp;</td>"
				+ "	<td>&nbsp;</td>"
				+ "	<td>&nbsp;</td>"
				+ "	<td>&nbsp;</td>"
				+ "	<td>&nbsp;</td>"
				+ "	<td>&nbsp;</td>"
				+ "	<td>&nbsp;</td>"
				+ "	<td>&nbsp;</td>"
				+ "	<td>&nbsp;</td>"
				+ "	<td>&nbsp;</td>"
				+ "	<td>&nbsp;</td>"
				+ "	<td>&nbsp;</td>"
				+ "	<td>&nbsp;</td>"
				+ "	<td>&nbsp;</td>"
				+ "	<td>&nbsp;</td>"
				+ "	<td>&nbsp;</td>"
				+ "	<td>" + Encode.forHtml(owner_name_1) + "</td>"
				+ "	<td>" + Encode.forHtml(owner_id_1) + "</td>"
				+ "	<td>&nbsp;</td>"
				+ "	<td>&nbsp;</td>"
				+ "	<td>&nbsp;</td>"
				+ "	<td>&nbsp;</td>"
				+ "	<td>" + Encode.forHtml(owner_name_2) + "</td>"
				+ "	<td>" + Encode.forHtml(owner_id_2) + "</td>"
				+ "	<td>&nbsp;</td>"
				+ "	<td>&nbsp;</td>"
				+ "	<td>&nbsp;</td>"
				+ "	<td>&nbsp;</td>"
				+ "	<td>" + Encode.forHtml(owner_name_3) + "</td>"
				+ "	<td>" + Encode.forHtml(owner_id_3) + "</td>"
				+ "	<td>&nbsp;</td>"
				+ "	<td>&nbsp;</td>"
				+ "	<td>&nbsp;</td>"
				+ "	<td>&nbsp;</td>"
				+ "</tr>";
				
				out.println(html);
				++n;
			}
		%>
		</tbody></table>
		
		
		
	</div>
</div>

<!-- Footer -->
<%@ include file="/jsp/common/Footer.jsp" %>	
<!-- End Footer -->

</body>
</html>